using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using HeroldIT.Dnn.SkinObjects;

namespace HeroldIT.Dnn.Modules.News.Views
{
    public class NewsDetail : ViewBase<NewsDetailContainer>, INewsDetailSettingsProvider
    {
        #region control fields

        

        #endregion

        #region properties

        private NewsEntry newsEntry;

        protected NewsEntry NewsEntry
        {
            get
            {
                if (null == this.newsEntry && this.HasCoreModule)
                {
                    int entryId = this.Controller.ResolveNewsEntryId(this.Request);
                    if (entryId > 0)
                    {
                        this.newsEntry = this.Controller.ReadNewsEntry(entryId, CultureInfo.CurrentUICulture.Name);
                    }
                }
                return this.newsEntry;
            }
        }

        private NewsEntry safeNewsEntry;

        protected NewsEntry SafeNewsEntry
        {
            get
            {
                if (null == this.safeNewsEntry)
                {
                    this.safeNewsEntry = this.NewsEntry;
                    if (null == this.safeNewsEntry)
                    {
                        this.safeNewsEntry = new NewsEntry();
                        this.safeNewsEntry.Details.Add(new NewsEntryDetail());
                    }
                }
                return this.safeNewsEntry;
            }
        }

        #endregion

        #region page lifecycle

        protected override void OnLoad(EventArgs e)
        {
            if (NewsController.AreSkinObjectsInstalled)
            {
                this.InitializeBreadcrumbRewriting();
            }

            base.OnLoad(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.HasCoreModule)
            {
                if (this.IsEditable)
                {
                    string noCoreModule = this.GetString("litNoCoreModule.Text");
                    if (String.IsNullOrEmpty(noCoreModule))
                    {
                        noCoreModule =
                            "No core module has been configured for this module. Please configure it before proceeding.";
                    }
                    writer.Write("<a href=\"{0}\">{1}</a>",
                                 Globals.NavigateURL(this.ModuleConfiguration.TabID, "Module",
                                                     new[] {"ModuleId=" + this.ModuleConfiguration.ModuleID}),
                                 noCoreModule);
                }
            }
            else
            {
                base.Render(writer);
            }
        }

        #endregion

        #region event handlers

        protected void btnDeleteEntry_Command(object sender, CommandEventArgs e)
        {
            int id;
            if (int.TryParse(e.CommandArgument as string, NumberStyles.Integer, CultureInfo.InvariantCulture, out id))
            {
                if (id > 0)
                {
                    this.DeleteNewsEntry(id);
                }
            }
        }

        private void breadcrumb_RenderingBreadcrumbPart(object sender, BreadcrumbEventArgs e)
        {
            if (e.TabId.Equals(this.PortalSettings.ActiveTab.TabID) && null != this.NewsEntry)
            {
                e.UrlToRender = this.GenerateDetailUrl(this.NewsEntry.NewsEntryId);
                e.NameToRender = this.NewsEntry.Details[0].Headline;
            }
        }

        #endregion

        #region methods

        private void InitializeBreadcrumbRewriting()
        {
            IList<BreadcrumbBase> breadcrumbs = BreadcrumbBase.GetBreadcrumbBasesFor(this.Page);
            if (null != breadcrumbs)
            {
                foreach (BreadcrumbBase breadcrumb in breadcrumbs)
                {
                    breadcrumb.RenderingBreadcrumbPart += this.breadcrumb_RenderingBreadcrumbPart;
                }
            }
        }

        protected string GenerateListUrl()
        {
            if (this.HasCoreModule)
            {
                return this.Controller.GenerateListUrl();
            }
            return null;
        }

        protected string GenerateDetailUrl(int newsEntryId)
        {
            if (this.HasCoreModule)
            {
                return this.Controller.GenerateDetailUrl(this.ModuleConfiguration.TabID, newsEntryId);
            }
            return null;
        }

        protected string GenerateEditUrl(int newsEntryId)
        {
            return this.ModuleBase.EditUrl("EntryId", newsEntryId.ToString(CultureInfo.InvariantCulture));
        }

        protected void DeleteNewsEntry(int newsEntryId)
        {
            if (this.HasCoreModule)
            {
                this.Controller.DeleteNewsEntry(newsEntryId);
                this.Response.Redirect(this.GenerateListUrl());
            }
        }

        #endregion
    }
}
